$(function() {
	
	$("#slider2").slider({
		value : 0,
		min : 0,
		max : 360,
		step : 1,
		animate : true,
		slide : function(event, ui) {
			$("#a2").val((ui.value) + "개월");
		}
	});
	$("#a2").val($("#slider2").slider("value") + "개월");


});

function doCalc() {
	var AddRowCount = 0;

	var cost = $("#a1").val().replace(/[^0-9]/gi, "");
	var term = $("#a2").val().replace(/[^0-9]/gi, "");
	var interestRate = $("#a3").val().replace(/[^0-9\.]/gi, "");
	var repaymentType = $("input:radio[name='ch']:checked").val();
	var repaymentCycle = 1;

	var col = "";
	var row = new Object();

	var totalInterest = 0;
	var avgInterest = 0;
	var totalRepayments = 0;
	var avgRepayments = 0;
	var repaymentTypeKor = "";

	var rest = cost;

	var principalSumTotal = 0;

	var tmpRepaymentCycle = 0;	
	var repaymentCnt = (Math.floor(term / repaymentCycle) == term / repaymentCycle) ? Math.round(Math.floor(term / repaymentCycle)) : Math.round(Math.floor(term / repaymentCycle)) + 1;

	if (repaymentType == '0') {
		repaymentTypeKor = "원리금균등상환";
		var repayments = (cost * (interestRate / 1200) * repaymentCycle) / (1 - 1 / (Math.pow((1 + (interestRate / 1200) * repaymentCycle), term / repaymentCycle)));

		for ( i = 0; i < repaymentCnt; i++) {
			row[i] = new Object();
			row[i]["repayments"] = repayments;
			row[i]["cnt"] = i + 1;

			tmpRepaymentCycle += repaymentCycle;

			if (tmpRepaymentCycle > term) {
				row[i]["interest"] = rest * (interestRate / 1200) * (term - (tmpRepaymentCycle - repaymentCycle));

				totalInterest += row[i]["interest"];

				row[i]["repayments"] = rest + row[i]["interest"];
			} else {
				row[i]["interest"] = rest * (interestRate / 1200) * repaymentCycle;

				totalInterest += row[i]["interest"];
			}

			row[i]["principalSum"] = row[i]["repayments"] - row[i]["interest"];

	
			row[i]["principalSumTotal"] = principalSumTotal += row[i]["principalSum"];

		
			row[i]["rest"] = rest -= row[i]["principalSum"];


			totalRepayments += row[i]["repayments"];
		}

	} else if (repaymentType == '1') {

		repaymentTypeKor = "원금균등상환";
		for ( i = 0; i < repaymentCnt; i++) {
			row[i] = new Object();
			tmpRepaymentCycle += repaymentCycle;
			row[i]["cnt"] = i + 1;

			
			if (tmpRepaymentCycle > term) {
			
				row[i]["interest"] = rest * (interestRate / 1200) * (term - (tmpRepaymentCycle - repaymentCycle));
			
				totalInterest += row[i]["interest"];
			
				row[i]["repayments"] = rest + row[i]["interest"];
			} else {
			
				row[i]["interest"] = rest * (interestRate / 1200) * repaymentCycle;
				
				totalInterest += row[i]["interest"];
			}

			if (repaymentCnt != i + 1) {
			
				row[i]["principalSum"] = cost / (term / repaymentCycle);
			} else {
				row[i]["principalSum"] = rest;
			}

			
			row[i]["repayments"] = row[i]["principalSum"] + row[i]["interest"];

			row[i]["rest"] = rest -= row[i]["principalSum"];

			row[i]["principalSumTotal"] = principalSumTotal += row[i]["principalSum"];

			
			totalRepayments += row[i]["repayments"];

		}

	} else if (repaymentType == '2') {
		repaymentTypeKor = "원금만기일시상환";

		for ( i = 0; i < repaymentCnt; i++) {
			row[i] = new Object();
			row[i]["cnt"] = i + 1;

			row[i]["interest"] = cost * (interestRate / 1200) * repaymentCycle;
			
			totalInterest = row[i]["interest"] * repaymentCnt;
			if (i + 1 == repaymentCnt) {
				row[i]["principalSum"] = cost;
				
				row[i]["repayments"] = Number(row[i]["principalSum"]) + Number(row[i]["interest"]);

			} else {
				row[i]["principalSum"] = 0;
				row[i]["repayments"] = Number(row[i]["principalSum"]) + Number(row[i]["interest"]);
			}
		
			row[i]["rest"] = rest -= row[i]["principalSum"];

		
			row[i]["principalSumTotal"] = principalSumTotal += row[i]["principalSum"];

			totalRepayments += row[i]["repayments"];
		}
	}

	avgInterest = Math.abs(Math.round(totalInterest / repaymentCnt));
	totalInterest = Math.abs(Math.round(totalInterest));
	avgRepayments = Math.abs(Math.round(totalRepayments / repaymentCnt));
	totalRepayments = Math.abs(Math.round(totalRepayments));

	var table1 = $("#table1 td");
	table1[0].innerHTML = addCommas(cost) + "원";

	table1[1].innerHTML = term + "개월";
	table1[2].innerHTML = interestRate + "%";
	table1[3].innerHTML = repaymentCycle + "개월";
	table1[4].innerHTML = repaymentTypeKor;
	table1[5].innerHTML = addCommas(Math.round(row[0]["repayments"])) + "원";
	table1[6].innerHTML = addCommas(Math.round(totalInterest)) + "원";

	if (repaymentCnt > 0) {
		$("#tbody").remove();
		$("#trbody").remove();
		$("#table2,#table1").addClass('table-bordered');
		$('<tbody id="tbody"></tbody>').appendTo("#table2");
	}

	var innerHtmlString = '<tr><td></td><td></td><td></td><td></td><td></td><td></td></tr>';
	for ( i = 0; i < repaymentCnt; i++) {
		$(innerHtmlString).appendTo("#tbody");
	}

	AddRowCount = repaymentCnt;

	var table2 = $("#table2 td");
	var didi=document.getElementById("div1");
	didi.innerHTML="";
	for(i=0; i<repaymentCnt; i++){
		
		didi.innerHTML += "<input type='TEXT'  name='CS_ST' style='background-color:#eaeaea;' value='1'/>";
		didi.innerHTML += "<input type='text'  name='CS_MN' value='2'>";
		didi.innerHTML += "<input type='text'  name='CS_OM' value='3'>";
		didi.innerHTML += "<input type='text'  name='CS_RM' value='4'>";
		didi.innerHTML += "<input type='text'  name='CS_SOM'value='5'>";
		didi.innerHTML += "<input type='text'  name='CS_SM' value='6'>";
		didi.innerHTML += "<input type='text'  name='CS_REM' value='7'>";
		didi.innerHTML += "<input type='text'  name='PD_PRD' value='8'>";
		didi.innerHTML += "<input type='text'  name='PD_RATE' value='9'>";
		didi.innerHTML += "<input type='text'  name='PD_PBACK' value='10'>";
		didi.innerHTML += "<input type='text'  name='CS_SRM' value='11'>";
	}
	
	var mn=document.getElementsByName('CS_MN');
	var st=document.getElementsByName('CS_ST');
	var om=document.getElementsByName('CS_OM');
	var rm=document.getElementsByName('CS_RM');
	var som=document.getElementsByName('CS_SOM');
	var sm=document.getElementsByName('CS_SM');
	var rem=document.getElementsByName('CS_REM');
	var prd=document.getElementsByName('PD_PRD');
	var rate=document.getElementsByName('PD_RATE');
	var pback=document.getElementsByName('PD_PBACK');
	var srm=document.getElementsByName('CS_SRM');
	var p=0;
	for ( i = 0; i < repaymentCnt; i++) {
		
		table2[(i * 6) + 0].innerHTML = row[i]["cnt"];													
		table2[(i * 6) + 1].innerHTML = addCommas(Math.round(row[i]["repayments"])) + "원";				
		table2[(i * 6) + 2].innerHTML = addCommas(Math.round(row[i]["principalSum"])) + "원";			
		table2[(i * 6) + 3].innerHTML = addCommas(Math.round(row[i]["interest"])) + "원";				
		table2[(i * 6) + 4].innerHTML = addCommas(Math.round(row[i]["principalSumTotal"])) + "원";		
		table2[(i * 6) + 5].innerHTML = addCommas(Math.round(row[i]["rest"])) + "원";
		
		st[p].value=table2[(p*6)+0].innerHTML;
		
		mn[p].value=table2[(p*6)+1].innerHTML;
		
		om[p].value=table2[p*6+2].innerHTML;
		
		rm[p].value=table2[p*6+3].innerHTML;
		som[p].value=table2[p*6+4].innerHTML;
		sm[p].value=table2[p*6+5].innerHTML;
		rem[p].value=table1[0].innerHTML;	
		prd[p].value=table1[1].innerHTML;
		rate[p].value=table1[2].innerHTML;
		pback[p].value=table1[4].innerHTML;
		srm[p].value=table1[6].innerHTML;
		p++;
	
		}
	
		
}